<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="SPU" prop="prodSpu">
        <el-input
          v-model="queryParams.prodSpu"
          placeholder="请输入SPU"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类型" prop="prodKind">
        <el-select
          v-model="queryParams.prodKind"
          placeholder="请选择商品类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.prod_kind"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商品状态" prop="prodStatus">
        <el-select
          v-model="queryParams.prodStatus"
          placeholder="请选择商品状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.prod_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称" prop="prodName">
        <el-input
          v-model="queryParams.prodName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['prod:prod:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['prod:prod:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['prod:prod:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['prod:prod:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="prodList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品ID" align="center" prop="prodId" />
      <el-table-column label="SPU" align="center" prop="prodSpu" />
      <el-table-column label="商品类型" align="center" prop="prodKind">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.prod_kind"
            :value="scope.row.prodKind"
          />
        </template>
      </el-table-column>
      <el-table-column label="商品分类" align="center" prop="prodType" />
      <el-table-column label="商品状态" align="center" prop="prodStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.prod_status"
            :value="scope.row.prodStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="prodName" />
      <el-table-column label="商品品牌" align="center" prop="brandName" />
      <el-table-column label="商品单位" align="center" prop="unitId" />
      <el-table-column label="商品标签" align="center" prop="tagId" />
      <el-table-column label="供应商" align="center" prop="appr" />
      <el-table-column
        label="缩略图"
        align="center"
        prop="prodImage"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview :src="scope.row.prodImage" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="供应商" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['prod:prod:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['prod:prod:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAddProdatta(scope.row)"
            v-hasPermi="['prod:prod:edit']"
            >添加商品附件</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleShowTbProdInfo(scope.row)"
            v-hasPermi="['prod:prod:remove']"
            >添加商品明细</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="SPU" prop="prodSpu">
          <el-input v-model="form.prodSpu" placeholder="请输入SPU" />
        </el-form-item>
        <el-form-item label="商品名称" prop="prodName">
          <el-input v-model="form.prodName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品类型" prop="prodKind">
          <el-select v-model="form.prodKind" placeholder="请选择商品类型">
            <el-option
              v-for="dict in dict.type.prod_kind"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品状态" prop="prodStatus">
          <el-select v-model="form.prodStatus" placeholder="请选择商品状态">
            <el-option
              v-for="dict in dict.type.prod_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品类型" prop="prodType">
          <el-cascader
            v-model="form.prodType"
            :options="tbProdTypeList"
            :props="cascaderProps"
            :show-all-levels="false"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="商品品牌" prop="brandId">
          <el-select v-model="form.brandId" placeholder="请选择商品品牌">
            <el-option
              v-for="item in tbBrandList"
              :key="item.brandId"
              :label="item.brandName"
              :value="parseInt(item.brandId)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品单位" prop="unitId">
          <el-input v-model="form.unitId" placeholder="请输入商品单位" />
        </el-form-item>
        <el-form-item label="商品标签" prop="tagId">
          <el-input v-model="form.tagId" placeholder="请输入商品标签" />
        </el-form-item>
        <el-form-item label="供应商" prop="appr">
          <el-input v-model="form.appr" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="缩略图" prop="prodImage">
          <image-upload
            v-model="form.prodImage"
            :limit="1"
            :auto-upload="false"
          />
        </el-form-item>
        <el-divider content-position="center">商品信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddTbProdInfo"
              >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteTbProdInfo"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="tbProdInfoList"
          :row-class-name="rowTbProdInfoIndex"
          @selection-change="handleTbProdInfoSelectionChange"
          ref="tbProdInfo"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column label="SKU" prop="prodSku" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.prodSku" placeholder="请输入SKU" />
            </template>
          </el-table-column>
          <el-table-column label="商品名称" prop="prodName" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.prodName"
                placeholder="请输入商品名称"
              />
            </template>
          </el-table-column>
          <el-table-column label="商品售价" prop="prodPrice" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.prodPrice"
                placeholder="请输入商品售价"
              />
            </template>
          </el-table-column>
          <el-table-column label="成本价" prop="costPrice" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.costPrice"
                placeholder="请输入成本价"
              />
            </template>
          </el-table-column>
          <el-table-column label="库存" prop="prodStock" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.prodStock"
                placeholder="请输入库存"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <div>
      <!-- 添加或修改商品信息对话框 -->
      <el-dialog
        :title="title"
        :visible.sync="openInfo"
        width="500px"
        append-to-body
      >
        <el-form
          ref="formInfo"
          :model="formInfo"
          :rules="rules"
          label-width="80px"
        >
          <el-form-item label="SKU" prop="prodSku">
            <el-input v-model="formInfo.prodSku" placeholder="请输入SKU" />
          </el-form-item>
          <el-form-item label="商品ID" prop="prodId">
            <el-input v-model="formInfo.prodId" placeholder="请输入商品ID" />
          </el-form-item>
          <el-form-item label="商品名称" prop="prodName">
            <el-input
              v-model="formInfo.prodName"
              placeholder="请输入商品名称"
            />
          </el-form-item>
          <el-form-item label="商品售价" prop="prodPrice">
            <el-input
              v-model="formInfo.prodPrice"
              placeholder="请输入商品售价"
            />
          </el-form-item>
          <el-form-item label="成本价" prop="costPrice">
            <el-input v-model="formInfo.costPrice" placeholder="请输入成本价" />
          </el-form-item>
          <el-form-item label="库存" prop="prodStock">
            <el-input v-model="formInfo.prodStock" placeholder="请输入库存" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitInfo">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

      <!-- 添加商品附件对话框 -->
      <el-dialog
        :title="titleAtta"
        :visible.sync="openAtta"
        width="500px"
        append-to-body
      >
        <el-form
          ref="formAtta"
          :model="formAtta"
          :rules="rulesAtta"
          label-width="80px"
        >
          <el-form-item label="商品ID" prop="prodId">
            <el-input
              v-model="formAtta.prodId"
              placeholder="请输入商品ID"
              disabled
            />
          </el-form-item>
          <el-form-item label="商品视频" prop="attaVideo">
            <file-upload v-model="formAtta.attaVideo" :file-type="['mp4']" />
          </el-form-item>
          <el-form-item label="上架标示" prop="upFlag">
            <el-switch
              v-model="formAtta.upFlag"
              :active-value="1"
              :inactive-value="0"
              active-text="定时上架"
              inactive-text="立即上架"
              style="display: block"
            />
          </el-form-item>
          <el-form-item
            label="定时上架时间"
            prop="upTime"
            v-if="formAtta.upFlag === 1"
          >
            <el-date-picker
              clearable
              v-model="formAtta.upTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择定时上架时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="定时下架" prop="downFlag">
            <el-switch
              v-model="formAtta.downFlag"
              :active-value="1"
              :inactive-value="0"
              active-text="是"
              inactive-text="否"
              style="display: block"
            />
          </el-form-item>
          <el-form-item
            label="定时下架时间"
            prop="downTime"
            v-if="formAtta.downFlag === 1"
          >
            <el-date-picker
              clearable
              v-model="formAtta.downTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择定时下架时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="轮播图片" prop="attaImages">
            <image-upload v-model="formAtta.attaImages" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitAttaForm">确 定</el-button>
          <el-button
            @click="
              () => {
                openAtta = false;
                resetAttaForm();
              }
            "
            >取 消</el-button
          >
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  listProd,
  getProd,
  delProd,
  addProd,
  updateProd,
  getProdListEs,
} from "@/api/prod/prod";
import { listType } from "@/api/prod/type";
import { listBrand } from "@/api/prod/brand";
import { addInfo, updateInfo } from "@/api/prod/info";
import { addAtta } from "@/api/prod/atta";
export default {
  name: "Prod",
  dicts: ["prod_status", "prod_kind"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTbProdInfo: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品管理表格数据
      prodList: [],
      // 商品信息表格数据
      tbProdInfoList: [],

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示商品明细弹窗弹出层
      openInfo: false,
      // 商品附件弹出层标题
      titleAtta: "",
      // 是否显示商品附件弹出层
      openAtta: false,
      // 商品附件表单参数
      formAtta: {
        attaId: null,
        prodId: null,
        prodName: null,
        attaVideo: null,
        upFlag: 0,
        upTime: null,
        downFlag: 0,
        downTime: null,
        attaImages: null,
      },
      // 表单校验
      rulesAtta: {
        attaVideo: [
          { required: true, message: "请上传商品视频", trigger: "change" },
        ],
      },
      // 商品类型数据
      tbProdTypeList: [],

      // 商品品牌数据
      tbBrandList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        prodSpu: null,
        prodKind: null,
        prodStatus: null,
        prodName: null,
      },
      cascaderProps: {
        checkStrictly: true,
        value: "typeId",
        label: "typeName",
        children: "children",
      },
      // 表单参数
      form: {},
      // 商品明细表单参数表单参数
      formInfo: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.getProdListEs();
    this.getTreeTypeList();
    this.getBrandList();
  },
  methods: {
    /** 查询商品管理列表 */
    getList() {
      this.loading = true;
      getProdListEs(this.queryParams).then((response) => {
        this.prodList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询商品分类列表 */
    getTreeTypeList() {
      let Params = {
        pageNum: 1,
        pageSize: 10000000,
      };
      listType(Params).then((response) => {
        this.tbProdTypeList = this.handleTree(
          response.data,
          "typeId",
          "typePid"
        );
      });
    },
    /** 查询品牌管理列表 */
    getBrandList() {
      let Params = {
        pageNum: 1,
        pageSize: 100,
      };
      listBrand(Params).then((response) => {
        this.tbBrandList = response.rows;
      });
    },

    //显示添加商品明细弹窗
    handleShowTbProdInfo(row) {
      this.reset();
      this.formInfo.prodId = row.prodId;
      this.formInfo.prodName = row.prodName;
      this.openInfo = true;
    },

    //添加商品明细按钮
    submitInfo() {
      addInfo(this.formInfo).then((response) => {
        this.$modal.msgSuccess("新增成功");
        this.openInfo = false;
        this.getList();
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.openInfo = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        prodId: null,
        prodSpu: null,
        prodKind: null,
        prodType: null,
        prodStatus: null,
        prodName: null,
        brandId: null,
        unitId: null,
        tagId: null,
        appr: null,
        prodImage: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.tbProdInfoList = [];
      this.resetForm("form");
      this.formInfo = {
        infoId: null,
        prodSku: null,
        prodId: null,
        prodName: null,
        prodPrice: null,
        costPrice: null,
        prodStock: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("formInfo");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.prodId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const prodId = row.prodId || this.ids;
      getProd(prodId).then((response) => {
        this.form = response.data;
        this.tbProdInfoList = response.data.tbProdInfoList;
        this.open = true;
        this.title = "修改商品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.tbProdInfoList = this.tbProdInfoList;
          if (this.form.prodType.length > 0) {
            this.form.prodType =
              this.form.prodType[this.form.prodType.length - 1];
          }
          if (this.form.prodId != null) {
            updateProd(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const prodIds = row.prodId || this.ids;
      this.$modal
        .confirm('是否确认删除商品管理编号为"' + prodIds + '"的数据项？')
        .then(function () {
          return delProd(prodIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 商品信息序号 */
    rowTbProdInfoIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 商品信息添加按钮操作 */
    handleAddTbProdInfo() {
      let obj = {};
      obj.prodSku = "";
      obj.prodName = "";
      obj.prodPrice = "";
      obj.costPrice = "";
      obj.prodStock = "";
      this.tbProdInfoList.push(obj);
    },
    /** 商品信息删除按钮操作 */
    handleDeleteTbProdInfo() {
      if (this.checkedTbProdInfo.length == 0) {
        this.$modal.msgError("请先选择要删除的商品信息数据");
      } else {
        const tbProdInfoList = this.tbProdInfoList;
        const checkedTbProdInfo = this.checkedTbProdInfo;
        this.tbProdInfoList = tbProdInfoList.filter(function (item) {
          return checkedTbProdInfo.indexOf(item.index) == -1;
        });
      }
    },
    /** 复选框选中数据 */
    handleTbProdInfoSelectionChange(selection) {
      this.checkedTbProdInfo = selection.map((item) => item.index);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "prod/prod/export",
        {
          ...this.queryParams,
        },
        `prod_${new Date().getTime()}.xlsx`
      );
    },

    /** 添加商品附件按钮操作 */
    handleAddProdatta(row) {
      this.resetAttaForm();
      this.formAtta.prodId = row.prodId;
      this.formAtta.prodName = row.prodName;
      this.openAtta = true;
      this.titleAtta = "添加商品附件";
    },

    /** 提交商品附件表单 */
    submitAttaForm() {
      this.$refs["formAtta"].validate((valid) => {
        if (valid) {
          addAtta(this.formAtta).then((response) => {
            this.$modal.msgSuccess("添加成功");
            this.openAtta = false;
          });
        }
      });
    },

    /** 重置商品附件表单 */
    resetAttaForm() {
      this.formAtta = {
        attaId: null,
        prodId: null,
        prodName: null,
        attaVideo: null,
        upFlag: 0,
        upTime: null,
        downFlag: 0,
        downTime: null,
        attaImages: null,
      };
      this.resetForm("formAtta");
    },
  },
};
</script>

<style lang="scss" scoped></style>
